import Rect from './rect'
/**
 * 游戏基础的方块类
 */
export default class Piece extends Rect {
    constructor(id, type, startx, starty, width, height, indexes) {
        super(startx, starty, width, height)
        this.id = id
        this.indexes = indexes
        this.isMoving = false
        this.movex = 0
        this.movey = 0
        this.speed = 1
        this.isHorizontal = this.width > this.height
        this.img = new Image()
        if (this.id == 0) {
            this.img.src = 'images/target.png'
        } else if (this.id > 0) {
            if (type == '0') {
                this.img.src = 'images/block0.png'
            } else if (type == '1') {
                this.img.src = 'images/block1.png'
            } else if (type == '2') {
                this.img.src = 'images/block2.png'
            } else if (type == '3') {
                this.img.src = 'images/block3.png'
            }
        }
    }

    /**
     * 操作释放后设置目标位置
     */
    moveTo(dx, dy) {
        this.isMoving = false
        this.locationx = this.startx + this.movex
        this.locationy = this.starty + this.movey
        if (this.isHorizontal) {
            this.startx += dx
            this.endx = this.startx + this.width
        } else {
            this.starty += dy
            this.endy = this.starty + this.height
        }
        this.movex = 0
        this.movey = 0
        this.isAuting = true
    }

    /**
     * 操作释放后自动移动到正确的位置
     */
    update() {
        if (!this.isAuting) {
            return
        }
        let delta = 0
        if (this.isHorizontal) {
            let targetx = this.startx
            delta = targetx - this.locationx
        } else {
            let targety = this.starty
            delta = targety - this.locationy
        }
        if (Math.abs(delta) < Math.abs(this.speed)) {
            this.isAuting = false
            this.speed = 1
        } else {
            if (delta > 0 && this.speed < 0) {
                this.speed = 1
            } else if (delta < 0 && this.speed > 0) {
                this.speed = -1
            }
            if (this.isHorizontal) {
                this.locationx = this.locationx + this.speed
            } else {
                this.locationy = this.locationy + this.speed
            }
            if (delta / this.speed > 32) {
                this.speed = this.speed * 2
            }
        }
    }

    /**
     * 将方块图绘制在canvas上
     */
    render(ctx) {
        if (this.isMoving) {
            ctx.drawImage(
                this.img,
                this.startx + this.movex,
                this.starty + this.movey,
                this.width,
                this.height
            )
        } else if (this.isAuting) {
            ctx.drawImage(
                this.img,
                this.locationx,
                this.locationy,
                this.width,
                this.height
            )
        } else {
            ctx.drawImage(
                this.img,
                this.startx,
                this.starty,
                this.width,
                this.height
            )
        }
    }
}